<template>
<div>
	<ol class="breadcrumb">
		<li class="breadcrumb-item">热门规则</li>
	</ol>
	<div class="biaoqing-container">
		<div class="biaoqing-table" v-if="rules.hotRule">
	      <table class="table table-bordered table-hover ">
	          <thead>
	            <tr>
	              <th>组</th>
	              <th>key</th>
	              <th>值</th>
	              <th>操作</th>
	            </tr>
	          </thead>
	          <tbody>
	            <tr v-for="(rule,index) in rules.hotRule">
	              <td class="max-width100" style="width:200px;">
	                <span class="biaoqing-table-content">{{rule.group}}</span>
	              </td>
	              <td class="max-width20">
	                <span class="biaoqing-table-content">{{rule.key}}</span>
	              </td>
	              <td class="view-num biaoqing-table-content max-width100">
	                <span>{{rule.value}}</span>
	              </td>
	              <td class="view-num cursor pass-success" @click="setRule(rule.id,rule.value)">
	                  修改
	              </td>
	            </tr>
	          </tbody>
	      </table>
	    </div>
    </div> 
    <ol class="breadcrumb">
		<li class="breadcrumb-item">热度值规则</li>
	</ol>
	<div class="biaoqing-container">
		<div class="biaoqing-table" v-if="rules.hotRule">
	      <table class="table table-bordered table-hover ">
	          <thead>
	            <tr>
	              <th>组</th>
	              <th>key</th>
	              <th>值</th>
	              <th>操作</th>
	            </tr>
	          </thead>
	          <tbody>
	            <tr v-for="(rule,index) in rules.heatRule">
	              <td class="max-width100" style="width:200px;">
	                <span class="biaoqing-table-content">{{rule.group}}</span>
	              </td>
	              <td class="max-width20">
	                <span class="biaoqing-table-content">{{rule.key}}</span>
	              </td>
	              <td class="view-num biaoqing-table-content max-width100">
	                <span>{{rule.value}}</span>
	              </td>
	              <td class="view-num cursor pass-success" @click="setRule(rule.id,rule.value)">
	                  修改
	              </td>
	            </tr>
	          </tbody>
	      </table>
	    </div>
    </div> 
    <ol class="breadcrumb">
		<li class="breadcrumb-item">敏感词管理<span class="color999" style="font-size:13px;">(点击敏感词删除)</span></li>
	</ol>
	<div class="biaoqing-container">
		<div class="tags-m-list pt-2 clearfloat">
    		<div v-for="(item,index) in sensitives.items" @click="deleteSensitives(item.id)" class="tags-m-name">{{item.text}}</div>
    		<div class="tags-m-name" @click="addSensitives"><i class="fa fa-plus-circle"></i></div>
    	</div>
	</div>
	<ol class="breadcrumb">
		<li class="breadcrumb-item">推送消息<span class="text-danger" style="font-size:13px;">(谨慎操作)</span></li>
	</ol>
	<div class="biaoqing-container">
		<div class="biaoqing-container">
			<div class="biaoqing-table">
				<div class="push-title flex">
					<span class="flex-item" style="font-size:15px;">推送历史</span>
					<span @click="pushTapShow" class="text-success cursor" style="font-size:15px;">设置推送</span>
				</div>
				<table class="table table-bordered ">
		          <thead>
		            <tr>
		              <th>内容</th>
		              <th>推送时间</th>
		              <th>类型</th>
		              <th>参数</th>
		            </tr>
		          </thead>
		          <tbody>
		            <tr v-for="(item,index) in pushList.items">
		              <td class="max-width100" style="width:200px;">
		                <span class="biaoqing-table-content" :title="item.content">{{item.content}}</span>
		              </td>
		              <td class="max-width20">
		                <span class="biaoqing-table-content">{{item.createTime}}</span>
		              </td>
		              <td class="view-num biaoqing-table-content max-width100">
		                <span>{{item.type}}</span>
		              </td>
		              <td class="view-num">
		                  {{item.param}}
		              </td>
		            </tr>
		          </tbody>
		      </table>
		      <div class="mask" :class="{'none':pushShow==false}">
		      	<div class="mask-bg" @click="pushTapShow"></div>
		      	<div class="push-main mask-main">
		      		<div class="push-main-title text-center">推送消息</div>
		      		<div class="push-main-content flex mt-3">
		      			<span>内容：</span>
		      			<textarea v-model="pushContent" placeholder="输入内容" class="flex-item"></textarea>
		      		</div>
		      		<div class="push-main-content flex mt-3">
		      			<span>标题：</span>
		      			<input v-model="pushTitle" placeholder="设置标题" class="flex-item" type="text">
		      		</div>
		      		<div class="push-main-content flex mt-3">
		      			<span>参数：</span>
		      			<input v-model="pushParam" placeholder="设置参数" class="flex-item" type="text">
		      		</div>
		      		<div class="push-main-content flex mt-3">
		      			<span>类型：</span>
		      			<select v-model="pushType">
		      				<option>1</option>
		      				<option>2</option>
		      			</select>
		      		</div>
		      		<div class="info color999 mt-2" style="font-size:13px;">类型1为作品，类型2为专题。当为类型1时参数填作品ID，为2时填专题名称</div>
		      		<div class="text-center mt-3">
		      			<span @click="setPush" class="btn btn-info cursor">确定</span>
		      		</div>
		      	</div>
		      </div>
			</div>
		</div>
	</div>
</div>
</template>
<script>
import '../../../static/css/biaoqing/tags.css'
import { Rule,Tags } from '../../resources'
import { formatTime } from '../../misc/utils'
import Pagepublic from '../../widgets/pagepublic.vue'
import swal2 from 'sweetalert2'
import querystring from 'querystring'

export default {
	data: () => ({
		loading: false,
		rules:'',
		pushList:"",
		sensitives:'',
		pushShow:false,
		pushType:1,
		pushContent:'',
		pushTitle:'',
		pushParam:''
	}),
	beforeRouteEnter (to,form,next) {
		Promise.all([Rule.list(),Tags.sensitive({pageSize:100}),Rule.push({pageSize:100})]).then(([rules,sensitives,pushData]) => {
    		next(vm => {
    			vm.rules =rules.data.data;
    			pushData.data.data.items.forEach(item => {
    				item.createTime=formatTime(item.createTime)
    			})
    			vm.pushList = pushData.data.data;
    			vm.sensitives = sensitives.data.data;
    		})
		})
	},
    mounted () {
     	this.$emit('loaded')
    },
    methods: {
    	setPush(){
    		var $this = this;
    		var content = this.pushContent;
    		var param = this.pushParam;
    		var type = this.pushType;
    		var title = this.pushTitle;
    		swal({
	          type: 'warning',
	          title: '您确定发送消息给全员?',
	          text: '谨慎操作!',
	          showCancelButton: true,
	          cancelButtonText: '取消',
	          confirmButtonText: '是的',
	          showLoaderOnConfirm: true,
	          closeOnConfirm: false
	        }, function(){
	        	if(content!='' && param!='' && type!='' ){
	        		var parems={content:content,param:param,type:type}
			        $this.$http.post('/push',parems).then(response => {
			            if(response.data.code==200){
			            	$this.loadPush();
			            	$this.pushContent='';
				    		$this.pushParam='';
				    		$this.pushTitle='';
				    		$this.pushType='';
				    		$this.pushShow=false;
			                $this.$notice.success(response.data.msg);
			            }else{
			              $this.$notice.error(response.data.msg);
			            }
			        })
			        swal.close()
	        	}else{
	        		swal.close()
	        		$this.$notice.error('信息不全');
	        	}
	        })
    	},
    	loadPush(){
    		var that = this;
    		Promise.all([Rule.push({pageSize:100})]).then(([pushData]) => {
    			pushData.data.data.items.forEach(item => {
    				item.createTime=formatTime(item.createTime)
    			})
    			that.pushList = pushData.data.data;
			})
    	},
    	pushTapShow(){
    		this.pushShow = !this.pushShow;
    	},
    	goRule(){
    		this.$http.get('/systemParam/subject/rule').then(res => {
    			this.rules =res.data.data;
    		})
    	},
    	loadSensitives(){
    		this.$http.get('/sensitive',{params:{pageSize:100}}).then(res=>{
    			this.sensitives = res.data.data
    		})
    	},
    	deleteSensitives(id){
    		var $this = this;
    		var parems={id:id}
    		swal({
	          type: 'warning',
	          title: '您确定?',
	          text: '删除的记录将不能恢复!',
	          showCancelButton: true,
	          cancelButtonText: '取消',
	          confirmButtonText: '是的, 我要删除!',
	          confirmButtonColor: '#DD6B55',
	          showLoaderOnConfirm: true,
	          closeOnConfirm: false
	        }, function(){
	          $this.$http.delete('/sensitive/'+id).then(response => {
	            if(response.data.code==200){
	            	$this.loadSensitives()
	                $this.$notice.success('删除成功');
	            }else{
	              $this.$notice.error('删除失败');
	            }
	          })
	          swal.close()
	        })
    	},
    	addSensitives(){
    		var $this = this;
	        var parems={}
	        swal2({
	          title: '添加敏感词',
	          input: 'text',
	          showCancelButton: true,
	          confirmButtonText: '确定',
	          cancelButtonText: '取消',
	          showLoaderOnConfirm: true,
	          reverseButtons:true,
	            preConfirm:function(text){
	              return new Promise(function(resolve,reject){
	                if(text !=''){
	                  parems.text=text;
	                  $this.$http.post('/sensitive',parems).then(res=>{
	                    if(res.data.code==200){
	                    	$this.loadSensitives()
	                      	$this.$notice.success(res.data.msg);
	                    }else{
	                      	$this.$notice.error(res.data.msg)
	                    }
	                  })
	                  swal2.close();
	                }else{
	                  reject('请输入内容');
	                }
	              })
	            }
	        }).catch(swal2.noop)
    	},
    	setRule(id,value){
    		var $this = this;
	        var parems={
	          "id": id
	        }
	        swal2({
	          title: '请输入修改的值',
	          text:'谨慎操作！',
	          input: 'text',
	          showCancelButton: true,
	          confirmButtonText: '确定',
	          cancelButtonText: '取消',
	          showLoaderOnConfirm: true,
	          inputValue:value,
	          reverseButtons:true,
	            preConfirm:function(value){
	              return new Promise(function(resolve,reject){
	                if(value !=''){
	                  parems.value=value;
	                  $this.$http.post('/systemParam',parems).then(res=>{
	                    if(res.data.code==200){
	                    	$this.goRule()
	                      	$this.$notice.success('修改成功');
	                    }else{
	                      	$this.$notice.error(res.data.msg)
	                    }
	                  })
	                  swal2.close();
	                }else{
	                  reject('请输入内容');
	                }
	              })
	            }
	        }).catch(swal2.noop)
    	}
    },
    components:{Pagepublic}
}
</script>
<style>
.push-main{
	padding: 10px;
	width:500px;
	height: 344px;
	left: 50%;
	top: 50%;
	margin-left: -250px;
	margin-top: -172px;
}
.push-main-content textarea{
	resize: none;
	outline: none;
	height: 60px;
	border-radius: 5px;
	border:1px solid #ddd;
	font-size: 14px;
}
.push-main-content  input{
	outline: none;
	padding: 5px;
	border-radius: 5px;
	border:1px solid #ddd;
	font-size: 14px;
}
</style>